package org.xxx.epms.inspector.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.xxx.epms.common.api.CommonPage;
import org.xxx.epms.common.api.CommonResult;
import org.xxx.epms.inspector.dto.InspectorDetailsDto;
import org.xxx.epms.inspector.dto.InspectorParam;
import org.xxx.epms.inspector.model.Inspector;
import org.xxx.epms.inspector.model.InspectorDetails;
import org.xxx.epms.inspector.service.InspectorService;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lb
 * @since 2024-06-08
 */

@Slf4j
@RestController
@RequestMapping("/inspector")
@RefreshScope
public class InspectorController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private InspectorService inspectorService;

    //获取反馈列表总页数
    @RequestMapping(value = "/getInspectorListTotalPage",method = RequestMethod.POST )
    @ApiOperation(value = "获取网格员列表总页数", notes = "获取网格员列表总页数")
    public CommonResult<Integer> getFeedbackListTotalPage(@RequestBody InspectorParam inspectorParam,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        int totalPage = inspectorService.getInspectorListTotalPage(inspectorParam, pageSize);
        return CommonResult.success(totalPage);
    };

    //分页获取反馈列表
    @RequestMapping(value = "/getInspectorList",method = RequestMethod.POST )
    @ApiOperation(value = "分页获取网格员列表", notes = "分页获取网格员列表")
    public CommonResult<CommonPage<Inspector>> getFeedbackList(@RequestBody InspectorParam inspectorParam,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<Inspector> page = inspectorService.getInspectorList(inspectorParam, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    };

    @RequestMapping(value = "/getInspectorByInsId",method = RequestMethod.GET )
    @ApiModelProperty(value = "根据网格员ID获取网格员信息", notes = "根据网格员ID获取网格员信息")
    public Inspector getInspectorByInsId(@RequestParam(value = "insId") Integer insId){
        return inspectorService.getInspectorByInsId(insId);
    }

    @RequestMapping(value = "/getInspectorByInsTel",method = RequestMethod.GET )
    @ApiModelProperty(value = "根据网格员手机号获取网格员信息", notes = "根据网格员手机号获取网格员信息")
    public Inspector getInspectorByInsTel(@RequestParam(value = "insTel") String insTel){
        return inspectorService.getInspectorByInsTel(insTel);
    }

    //注册用户接口
    @RequestMapping(value = "/register",method = RequestMethod.POST )
    @ApiOperation(value = "注册用户", notes = "注册用户")
    public CommonResult<Inspector> register(@Validated @RequestBody InspectorParam inspectorParam){
        Inspector inspector = null;
        try {
           inspector = inspectorService.register(inspectorParam);
        }catch (Exception e){
            return CommonResult.failed(e.getMessage());
        }
        if (inspector == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(inspector);
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST )
    @ApiOperation(value = "网格员登录", notes = "网格员登录")
    public CommonResult login(@Validated @RequestBody InspectorParam inspectorParam){
        log.info("login param: " + inspectorParam.toString());
        String token = null;
        try{
            token = inspectorService.login(inspectorParam);
        }catch (Exception e){
            return CommonResult.failed(e.getMessage());
        }
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
//        log.info("login success, token: " + token);
        return CommonResult.success(tokenMap);
    }

    //获取用户信息接口
    @RequestMapping(value = "/info",method = RequestMethod.POST )
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public CommonResult<Inspector> getInsInfo(@RequestBody InspectorParam inspectorParam){
        Inspector inspector = inspectorService.getInspectorByInsId(inspectorParam.getInsId());
        if (inspector == null) {
            return CommonResult.failed("用户不存在");
        }
        inspector.setInsPassword(null);
        return CommonResult.success(inspector);
    }

    @RequestMapping(value = "/getInsDetails", method = RequestMethod.GET)
    public InspectorDetailsDto getInsDetails(@RequestParam("insId") Integer insId){
        InspectorDetails inspectorDetails = inspectorService.loadUserByUsername(insId);
        return new InspectorDetailsDto(inspectorDetails);
    }
}

